import re
from playwright.sync_api import Playwright, sync_playwright, expect


def run(playwright: Playwright) -> None:
    browser = playwright.chromium.launch(headless=False)
    context = browser.new_context()
    page = context.new_page()
    page.goto("https://www.baidu.com/")
    page.get_by_role("textbox", name="大马士革一教堂现自杀式袭击").click()
    page.get_by_role("textbox", name="大马士革一教堂现自杀式袭击").fill("bilibili")
    page.get_by_role("textbox", name="大马士革一教堂现自杀式袭击").press("Enter")
    with page.expect_popup() as page1_info:
        page.locator("#content_left [id=\"\\31 \"]").get_by_role("link", name="哔哩哔哩 (゜-゜)つロ 干杯~-bilibili").click()
    page1 = page1_info.value
    page1.get_by_role("textbox", name="数学林老师").click()
    page1.get_by_role("textbox", name="数学林老师").fill("萌娘百科")
    with page1.expect_popup() as page2_info:
        page1.get_by_role("textbox", name="数学林老师").press("Enter")
    page2 = page2_info.value
    page2.locator(".close > svg").click()
    page2.get_by_role("button", name="下一页").click()
    page2.get_by_role("button", name="下一页").click()
    page2.get_by_role("button", name="下一页").click()

    # ---------------------
    context.close()
    browser.close()


with sync_playwright() as playwright:
    run(playwright)
